package com.example.recyclerviewproject;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewAct2 extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview_layout2);
        // No Titlebar
        getSupportActionBar().hide();
        //*初始化RecyclerView控件
        RecyclerView mRecyclerView = findViewById(R.id.mRecyclerView);
        SmartRefreshLayout smartRefreshLayout = findViewById(R.id.refreshLayout);
        //*创建布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(RecyclerView. VERTICAL);
        //水平
      //  layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        //网格布局
      //  GridLayoutManager layoutManager2 = new GridLayoutManager(this,3);
        //*设置布局管理器
        mRecyclerView.setLayoutManager(layoutManager);
        //准备数据
        List<ItemBean> list = new ArrayList<>();
        for (int i = 1; i <=40; i++) {
            ItemBean itemBean = new ItemBean();
            itemBean.setImageInt(R.mipmap.new_icon);
            itemBean.setTitle("标题"+i);
            itemBean.setContent("内容===="+i);
            list.add(itemBean);
        }
        //*创建适配器
        MyAdapter myAdapter = new MyAdapter(this);
        //*给适配器传数据作为列表数据
        myAdapter.setList(list);
        //*设置RecyclerView的适配器
        mRecyclerView.setAdapter(myAdapter);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, RecyclerView.VERTICAL);
        mRecyclerView.addItemDecoration(dividerItemDecoration);

        //刷新
        smartRefreshLayout.setRefreshHeader(new ClassicsHeader(this));
        smartRefreshLayout.setRefreshFooter(new ClassicsFooter(this));
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                list.clear();
                for (int i = 1; i <=40; i++) {
                    ItemBean itemBean = new ItemBean();
                    itemBean.setImageInt(R.mipmap.new_icon);
                    itemBean.setTitle("标题===="+i);
                    itemBean.setContent("内容~~~~~~~~~~"+i);
                    list.add(itemBean);
                }
                myAdapter.setList(list);
                myAdapter.notifyDataSetChanged();
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                for (int i = 41; i <=51; i++) {
                    ItemBean itemBean = new ItemBean();
                    itemBean.setImageInt(R.mipmap.new_icon);
                    itemBean.setTitle("标题=+=+="+i);
                    itemBean.setContent("内容~~+~~+~~~~~~"+i);
                    list.add(itemBean);
                }
                myAdapter.notifyDataSetChanged();
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });




    }
}
